//
//  SearchTable2ViewController.m
//  Boom
//
//  Created by 云鹏 赵 on 15/3/13.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SearchTable2ViewController.h"

@interface SearchTable2ViewController ()

@end

@implementation SearchTable2ViewController
- (void)dealloc
{
    [_albumArray2 release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.albumArray2 = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    [self afnGetData:self.id page:1];
    [self addFooter];
}
- (void)addFooter
{
    __block SearchTable2ViewController *vc = self;
    
    [self.myTableView addFooterWithCallback:^{
        
        [vc afnGetData:self.id page:self.nextPage];
        
    }];

}
// afn获取数据(拼接id)
- (void)afnGetData:(NSInteger) albumId page:(NSInteger) page
{
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%d/true/%d/30?device=iPhone", albumId, page];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        self.nextPage = page + 1;
        
        NSMutableDictionary *allDic = responseObject;
        NSMutableDictionary *dictionary = [allDic objectForKey:@"tracks"];
        NSMutableArray *array = [dictionary objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            SearchAlbum2 *searchAlbum2 = [[SearchAlbum2 alloc] init];
            [searchAlbum2 setValuesForKeysWithDictionary:dic];
            [self.albumArray2 addObject:searchAlbum2];
            NSLog(@"%@", self.albumArray2);
        }
        [self.myTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.albumArray2.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdetifier = @"twice";
    Search2TableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdetifier];
    if (cell == nil) {
        cell = [[Search2TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];
        [cell autorelease];
    }
    cell.searchAlbum2 = [self.albumArray2 objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[PlayerViewController sharePlayerDataHandel] streamerStop];
    SearchAlbum2 *searchAlbum2 = [self.albumArray2 objectAtIndex:indexPath.row];
    [self afnGetData:searchAlbum2.trackId];
}
// afn获取数据
- (void)afnGetData:(NSInteger) albumId
{
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=%d", albumId];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *dictionary = responseObject;
        PlayData *playData = [[PlayData alloc] init];
        [playData setValuesForKeysWithDictionary:dictionary];
        [PlayerViewController sharePlayerDataHandel].playData = playData;
        [[PlayerViewController sharePlayerDataHandel] buttonPressed];
        [playData release];
        [self.navigationController presentViewController:[PlayerViewController sharePlayerDataHandel] animated:YES completion:^{
            
            
        }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
